package com.zking.dao;

import com.zking.dao.ProductDao;
import com.zking.entity.Product;
import com.zking.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class ProductDao {
    public static List<Product> selectProductByPage(int pageIndex, int pageSize, int classId) {
        String sql = "select * from easybuy_product where ep_file_name!='deleted'";
        List<Object> params = new ArrayList<>();
        if(classId!=0){
            sql += " and epc_id=?";
            params.add(classId);
        }
        sql += " limit ?,? ";
        params.add((Math.max(0, pageIndex-1)*pageSize));
        params.add(pageSize);

        return DBUtil.query(sql, Product.class, params.toArray());
    }

    public static List<Product> selectAllProduct() {
        String sql = "select * from easybuy_product where ep_file_name!='deleted'";
        return DBUtil.query(sql, Product.class);
    }

    public static List<Product> selectProductByClass(int parentClassId){
        String sql = "select * from easybuy_product where ep_file_name!='deleted' and epc_id=?";
        return DBUtil.query(sql, Product.class, parentClassId);
    }

    public static int getMaxPage(int pageSize, int parentClassId) {
        List<Product> list;
        if(parentClassId!=0){
            list = selectProductByClass(parentClassId);
        }else{
            list = selectAllProduct();
        }

        int total = list.size();
        int maxPage = total/pageSize;
        if(total%pageSize!=0){
            maxPage++;
        }
        return maxPage;
    }

    public static Product selectProductById(int ep_id) {
        String sql = "select * from easybuy_product where ep_id=? and ep_file_name!='deleted'";
        List<Product> list = DBUtil.query(sql, Product.class, ep_id);
        if(list.size()==1){
            return list.get(0);
        }
        return null;
    }

    public static boolean updateStock(int ep_id, int stock) {
        String sql = "update easybuy_product set ep_stock=ep_stock-? where ep_id=? and ep_file_name!='deleted'";
        return DBUtil.update(sql,stock,ep_id);
    }

    public static boolean updateProduct(Product product){
        String sql = "update easybuy_product set ep_name=?,ep_description=?,ep_price=?,ep_stock=?,epc_id=?,epc_child_id=?";

        if(product.getEp_image_bytes()!=null){
            sql += ",ep_image_bytes=?";
            System.out.println(product.getEp_image_bytes().length);
            return DBUtil.update(sql+" where ep_id=? and ep_file_name!='deleted'",
                    product.getEp_name(), product.getEp_description(), product.getEp_price(),
                    product.getEp_stock(), product.getEp_id(), product.getEpc_child_id(), product.getEp_image_bytes(),
                    product.getEp_id());
        }else {
            return DBUtil.update(sql+" where ep_id=? and ep_file_name!='deleted'",
                    product.getEp_name(), product.getEp_description(), product.getEp_price(),
                    product.getEp_stock(), product.getEpc_id(), product.getEpc_child_id(), product.getEp_id());
        }
    }

    // 假删除
    public static boolean deleteProduct(int id){
        String sql = "update easybuy_product set ep_file_name=? where ep_id=?";
        return DBUtil.update(sql, "deleted", id);
    }

    public static boolean addProduct(Product product){
        String sql = "INSERT INTO easybuy_product (ep_name, " +
                "ep_description, ep_price, ep_stock, ep_file_name, ep_image_bytes, epc_id, epc_child_id) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

        return DBUtil.update(sql, product.getEp_name(), product.getEp_description(), product.getEp_price(),
                product.getEp_stock(), "", product.getEp_image_bytes(), product.getEpc_id(), product.getEpc_child_id());
    }
}
